package com.college.controller.products;

import com.college.common.constant.ErrorMsg;
import com.college.common.pojo.ao.IdleItem;
import com.college.common.pojo.vo.ResultVo;
import com.college.common.utils.UserUtils;
import com.college.products.service.IdleItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@RestController
@RequestMapping("idle")
@Api(tags = "闲置商品")
@Slf4j
public class IdleItemController {

    @Autowired
    private IdleItemService idleItemService;

    @ApiOperation(value = "新增闲置")
    @PostMapping("add")
    public ResultVo addIdleItem(@RequestBody IdleItem idleItem){
        Long shUserId = UserUtils.getHeaderUserId();
        idleItem.setUserId(shUserId);
        idleItem.setIdleStatus((byte) 1);
        idleItem.setReleaseTime(new Date());
        if(idleItemService.addIdleItem(idleItem)){
            return ResultVo.success(idleItem);
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "获取闲置信息")
    @GetMapping("info")
    public ResultVo getIdleItem(@RequestParam Long id){
        return ResultVo.success(idleItemService.getIdleItem(id));
    }

    @ApiOperation(value = "查询所有闲置")
    @GetMapping("all")
    public ResultVo getAllIdleItem(){
        Long shUserId = UserUtils.getHeaderUserId();
        return ResultVo.success(idleItemService.getAllIdelItem(shUserId));
    }

    @ApiOperation(value = "搜索闲置")
    @GetMapping("find")
    public ResultVo findIdleItem(@RequestParam(value = "findValue",required = false) String findValue,
                                 @RequestParam(value = "page",required = false) Integer page,
                                 @RequestParam(value = "nums",required = false) Integer nums){
        if(StringUtils.isBlank(findValue)){
            findValue="";
        }
        int p=1;
        int n=8;
        if(null!=page){
            p=page>0?page:1;
        }
        if(null!=nums){
            n=nums>0?nums:8;
        }
        return ResultVo.success(idleItemService.findIdleItem(findValue,p,n));
    }

    @ApiOperation(value = "查询闲置分页")
    @GetMapping("lable")
    public ResultVo findIdleItemByLable(@RequestParam(value = "idleLabel",required = true) Integer idleLabel,
                                 @RequestParam(value = "page",required = false) Integer page,
                                 @RequestParam(value = "nums",required = false) Integer nums){
        int p=1;
        int n=8;
        if(null!=page){
            p=page>0?page:1;
        }
        if(null!=nums){
            n=nums>0?nums:8;
        }
        return ResultVo.success(idleItemService.findIdleItemByLable(idleLabel,p,n));
    }

    @ApiOperation(value = "更新闲置")
    @PostMapping("update")
    public ResultVo updateIdleItem(@RequestBody IdleItem idleItem){
        Long shUserId = UserUtils.getHeaderUserId();
        idleItem.setUserId(shUserId);
        if(idleItemService.updateIdleItem(idleItem)){
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }
}
